﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Dormsystem
{
    public partial class AddUser : Form
    {
        public AddUser()
        {
            InitializeComponent();
        }

        private Boolean ValidData()
        {
            if ( txtUserName.Text == "" )
            {
                MessageBox.Show("用户名不能为空");
                return false;
            }

            if ( txtPassword.Text != txtPassword2.Text )
            {
                MessageBox.Show("两次密码输入不一致");
                return false;
            }

            string user = txtUserName.Text;

            string sql = string.Format("select count(*) from userinfo where username='{0}';", user);

            int cnt = Convert.ToInt32(DB.GetScalar(sql));

            if (cnt > 0)
            {
                MessageBox.Show("已经存在该用户");
                return false;
            }
 
            return true;

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string name = txtUserName.Text;
            string pwd = txtPassword.Text;
            string state = cboState.Text;

            if (!ValidData())
            {
                return;
            }
            
            string sql = string.Format("insert into userinfo (username,password,userstate)"
                + "values('{0}','{1}','{2}')", name,pwd,state);

            int result = DB.ExcuteSQL(sql);

            if (result > 0)
            {
                MessageBox.Show("添加成功");
                txtUserName.Text = "";
                txtPassword.Text = "";
                txtPassword2.Text = "";
                cboState.Text = "";
            }

        }
    }
}
